<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户</label>
                            <div class="layui-input-inline">
                                <input type="text" name="username" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
            </div>
        </script>
        <table class="layui-hide" id="TableId_User" lay-filter="TableFilter_User"></table>
        <script type="text/html" id="switchTpl">
            <input type="checkbox" name="state" value="{{d.id}}" lay-skin="switch" lay-text="正常|冻结" lay-filter="StateControl" {{ d.ban == 0?'checked' : '' }}>
        </script>
    </div>
</div>
<div id="user_mtk" style="display: none;">
    <div class="layui-card">
        <div class="layui-card-body" style="padding-top: 40px;">
            <div class="layui-form" action="" lay-filter="example">
                <input type="text" name="id" style="display: none;">
                <div class="layui-form-item">
                    <label class="layui-form-label">账号</label>
                    <div class="layui-input-block">
                        <input type="text" name="username" lay-verify="username_verification" lay-reqtext="数据是必填项，岂能为空？" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">密码</label>
                    <div class="layui-input-block">
                        <input type="text" name="passwrod" lay-verify="passwrod_verification" lay-reqtext="邮箱是必填项，岂能为空？" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <button class="layui-btn" lay-submit lay-filter="user_submit">提交</button>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#TableId_User',
            url: '/SelectUserListPage',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            headers: {"token":localStorage.getItem("token")},
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 80, title: 'ID', hide: true},
                {field: 'username', width: 110, title: '用户名', sort: true},
                {field: 'password', width: 120, title: '密码', edit: 'text'},
                {field: 'permission', width: 80, title: '权限', sort: true, edit: 'text'},
                {field: 'quota', width: 90, title: '可挂数', sort: true, edit: 'text'},
                {field: 'money', title: '钱(保留)', width: 100, sort: true, edit: 'text'},
                {field: 'registertime', width: 160, title: '注册时间', sort: true, edit: 'text'},
                {field: 'duedate', width: 160, title: '到期时间', sort: true, edit: 'select'},
                {field: 'other', minWidth: 150, title: '其他', edit: 'text'},
                {field: 'ban', width: 100, title: '状态',templet: '#switchTpl', unresize: true}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            table.reload('TableId_User', {
                page: {curr: 1}
                , where: {username: data.field.username}
            }, 'data');
            return false;
        });
        //监听状态操作
        form.on('switch(StateControl)', function(obj){
            $.ajax({url: "/UpdateUserBanInversion",
                data: "id="+this.value,type: "POST",
                headers: {"token":localStorage.getItem("token")},
                async: false, cache: false,
                success: function (data) {
                    if(data==0){
                        layer.msg("切换成功！");
                    }else {
                        layer.msg("切换失败！");
                    }
                },error: function (e) {layer.msg("请求错误！");}
            });
        });
        /**
         * toolbar监听事件
         */
        table.on('toolbar(TableFilter_User)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加用户',
                    type: 1,
                    anim: 4,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: $("#user_mtk"),
                });
                form.val('example', {
                    "username": ""
                    ,"passwrod": ""
                })
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {
                var checkStatus = table.checkStatus('TableId_User')
                    , data = checkStatus.data;
                layer.confirm('真的删除行么', function (index) {
                    layer.close(index);
                    for (let i = 0; i <data.length ; i++) {
                        $.ajax({url: "/DeleteUser",data: "id="+data[i].id,type: "POST",
                            headers: {"token":localStorage.getItem("token")},
                            async: false,cache: false,
                            success: function (data) {
                                if(data==0){
                                    layer.msg("删除成功！");
                                    setTimeout('window.location.reload()',1000);
                                }else {
                                    layer.msg("删除失败！");
                                }
                            },error: function (e) {layer.msg("请求错误！");}
                        });
                    }
                });
            }
        });
        //监听提交
        form.on('submit(user_submit)',function (datas){
            var data = datas.field;
            $.ajax({url: "/AddUser",
                data: "username="+data.username+"&passwrod="+data.passwrod,
                type: "POST",headers: {"token":localStorage.getItem("token")},
                async: false,cache: false,
                success: function (data) {
                    if(data==0){
                        layer.msg("添加成功！");
                        setTimeout('window.location.reload()',1000);
                    }else {
                        layer.msg("添加失败,用户可能已存在。");
                    }
                }, error: function (e) {layer.msg("请求错误！");}
            });
            return false;
        });
        //监听单元格编辑
        table.on('edit(TableFilter_User)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            // layer.alert(JSON.stringify(data));
            $.ajax({url: "/UpdateUser",
                data: JSON.stringify(data),type: "POST",dataType: "json",
                headers: {"token":localStorage.getItem("token"),"Content-Type":"application/json"},
                async: false,cache: false,
                success: function (data) {
                    if(data==0){
                        layer.msg("修改成功！");
                        setTimeout('window.location.reload()',1000);
                    }else {
                        layer.msg("修改失败！");
                    }
                },error: function (e) {layer.msg("请求错误！");}
            });
        });
        //自定义验证规则
        form.verify({
            passwrod_verification:function (value) {
                if(value===""){
                    return '数据不得为空';
                }
            }, username_verification: [
                /^\w+@\w{2,3}\.\w{2,6}$/
                , '邮箱格式错误！'
            ], content: function (value) {
                layedit.sync(editIndex);
            }
        });
    });
</script>
</body>
</html>